/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfirmArchives.java
 *
 * Created on Aug 3, 2011, 8:17:15 PM
 */
package invento;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author Kristina
 */
public class ConfirmArchives extends javax.swing.JFrame {
    JFrame parent;
    JTable tab;
    DbConnector dbConn = new DbConnector();

    private int rowCount;
    private int colCount;
    private int rowPrint;
    private int rowLoop;
    private String strPrimary;
    private String strCompare;


    /** Creates new form ConfirmArchives */
    public ConfirmArchives(JFrame parent, JTable table, String type) {
        initComponents();
        this.setTitle("Archives Confirmation");
        this.parent = parent;
        tab = table;
        dbConn.dbConnect();
        
        String array[] = new String[100];
        int row, col, i;
        
        row = tab.getSelectedRow();
        col = 0;
        
        if(type == "firearm"){
            for(i=0; i<11; i++){
                array[i] =  "\"" + tab.getValueAt(row, col).toString() + "\"";
                col++;
            }
        
            try{                
                dbConn.statement.executeUpdate("INSERT INTO inventorysystem.firearmarchive (Category, Status, Type, Make, Calibre, SerialNo, PropertyNo, AcquisitionDate, AcquisitionCost, RepairCost, Source) VALUES ("
                        + array[1] + "," + array[2] + ","+ array[3] + ","+ array[4] + ","+ array[5]+ "," + array[6]+ "," + array[0]+ "," + array[7]+ "," + array[8]+ "," + array[9]+ "," + array[10] + ")");
                System.out.println(array[0]);
                dbConn.statement.executeUpdate("DELETE FROM inventorysystem.firearm WHERE PropertyNo = '" +tab.getValueAt(row, 0).toString()+ "'");
                
            }catch(Exception e){
                e.printStackTrace();
            }finally{   
                    try{    
                            dbConn.statement.close();
                            dbConn.conn.close();
                    }
                    catch(Exception e){
                    e.printStackTrace();
                    }

                }
            dbConn.dbConnect();
            dbConn.displayFirearm(tab);
            }
        }
 

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jDialog2 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        buttonConfirmYes = new javax.swing.JButton();
        buttonCancelSendtoArch = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog2Layout.setVerticalGroup(
            jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Send to Archives?");

        buttonConfirmYes.setText("Yes");
        buttonConfirmYes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonConfirmYesMouseClicked(evt);
            }
        });

        buttonCancelSendtoArch.setText("Cancel");
        buttonCancelSendtoArch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonCancelSendtoArchMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(buttonConfirmYes)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buttonCancelSendtoArch)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonConfirmYes)
                    .addComponent(buttonCancelSendtoArch))
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void buttonConfirmYesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonConfirmYesMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
        
        SuccessfulArchives successarchiveswindow = new SuccessfulArchives(parent);
        successarchiveswindow.setVisible(true);
        successarchiveswindow.setLocationRelativeTo(null);
        
    }//GEN-LAST:event_buttonConfirmYesMouseClicked
    
    private void buttonCancelSendtoArchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonCancelSendtoArchMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
        JOptionPane.showMessageDialog(this, "Send to Archive Failed.",
        "Properties Not Sent to Archive", JOptionPane.ERROR_MESSAGE);
        returntomain();
    }//GEN-LAST:event_buttonCancelSendtoArchMouseClicked

    
     public void returntomain(){
    
        this.setVisible(false);
        this.dispose();
        parent.setEnabled(true);
        parent.requestFocus();
        
    }
     
    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancelSendtoArch;
    private javax.swing.JButton buttonConfirmYes;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
